#include "workthread.h"
#include "agreement/agreement.h"


uint8_t workThread::_stop = 0;
enum work_mode workThread::_mode=WORK_CLIENT;
QString workThread::hostName="";
quint16 workThread::port=0;

workThread::workThread(QObject *parent) : QObject(parent)
{

}

void workThread::doWork(const QString &parameter)
{
#if 0
    (void)parameter;
    qDebug() << "client run";
    QString result("Done!");
    uint32_t count=0;
    socket_client = new SocketClient(this);
    for (count=0;count<50;count++) {
        QThread::msleep(100);
        //qDebug() << "run--"+QString::number(count);
        emit resultReady("run--"+QString::number(count));
    }
    /* ... here is the expensive or blocking operation ... */
    emit resultReady(result);
    qDebug() << "client exit";

    delete socket_client;
    socket_client = nullptr;
    //this->exit(0);

#endif
    (void)parameter;
    switch (_mode)
    {
        case WORK_CLIENT:     // client
            client();
            break;
        case WORK_LOCAL:      // local
            local();
            break;
        case WORK_DOWNLOAD:   // download
            download();
            break;
        default:
            break;
    }

}

void workThread::client()
{
    qDebug() << "client run";
    QString result("Done!");
    uint32_t count=0;
    socket_client = new SocketClient(hostName, port, this);
#if 1
        socket_client->login();
        QThread::msleep(2000);
        socket_client->UTC();
        QThread::msleep(3000);
        socket_client->logout();
        QThread::msleep(3000);
#endif
    while(0==_stop)
    {
        QThread::msleep(1000);
        //qDebug() << "run--"+QString::number(count);
        emit resultReady("run--"+QString::number(count));
        count++;
#if 0
        socket_client->login();
        QThread::msleep(1000);
        socket_client->UTC();
        QThread::msleep(1000);
        socket_client->logout();
        QThread::msleep(1000);
        socket_client->report();
        QThread::msleep(1000);
        socket_client->report_later();
#else
        socket_client->report();
        QThread::msleep(1000);
        socket_client->report_later();
#endif
    }
    /* ... here is the expensive or blocking operation ... */
    emit resultReady(result);
    qDebug() << "client exit";

    delete socket_client;
    socket_client = nullptr;
    //this->exit(0);
}

void workThread::download()
{
    qDebug() << "download run";
    QString result("Done!");
    uint32_t count=0;
    socket_client = new SocketClient(hostName, port, this);
#if 1
    socket_client->login();
    QThread::msleep(500);
    socket_client->UTC();
    QThread::msleep(500);
    socket_client->logout();
    QThread::msleep(500);
#endif
    while(0==_stop)
    {
        QThread::msleep(10);
        //qDebug() << "run--"+QString::number(count);
        emit resultReady("run--"+QString::number(count));
        count++;
#if 0
        socket_client->login();
        QThread::msleep(1000);
        socket_client->UTC();
        QThread::msleep(1000);
        socket_client->logout();
        QThread::msleep(1000);
        socket_client->report();
        QThread::msleep(1000);
        socket_client->report_later();
#else
        socket_client->upload();
#endif
    }
    /* ... here is the expensive or blocking operation ... */
    emit resultReady(result);
    qDebug() << "client exit";

    delete socket_client;
    socket_client = nullptr;
    //this->exit(0);
}

void workThread::local()
{
    qDebug() << "local run";
    QString result("Done!");
    uint32_t count=0;
    while(0==_stop)
    {
        QThread::msleep(1000);
        //qDebug() << "run--"+QString::number(count);
        emit resultReady("run--"+QString::number(count));
        count++;
        agreement_test();
    }
    /* ... here is the expensive or blocking operation ... */
    emit resultReady(result);
    qDebug() << "client exit";
    //this->exit(0);
}
